package com.cvit.controller;

import java.beans.IntrospectionException;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cvit.model.Msg;
import com.cvit.model.Postal;
import com.cvit.service.PostalService;
import com.cvit.utils.Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class PostalController {
	
		@Autowired
		PostalService postalService;
		
		/**
		 * 查询邮单表
		 * */
		@RequestMapping("/getPostal")
		@ResponseBody
		public Msg getPostals(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
			PageHelper.startPage(pn, 5);
			List<Postal> postals = postalService.getPostal();
			PageInfo<Postal> page = new PageInfo<Postal>(postals, 5);
			return Msg.success().add("pageInfo", page);
	}
		
		/**
		 * 根据ID、考生号、身份证号、名字查询
		 * */
		@RequestMapping("/getPostalBy")
		@ResponseBody
		public Msg getPostalBy(Postal postal){
			Postal postals = postalService.getPostalBy(postal);
			return Msg.success().add("Postals",postals);
	}
		
		/**
		 * 根据身份证号查询
		 * */
		@RequestMapping("/getPostalByCardId")
		@ResponseBody
		public Msg getPostalByCard(String cardId){
			Postal postals = postalService.getPostalByCard(cardId);
			if(Utils.isNull(postals)==true){
				return Msg.fail().add("code", "身份证号填写错误");//查询为空
			}
			return Msg.success().add("Postals",postals);
	}
		
		
		/**
		 * 导入Excel
		 * */
		@RequestMapping("/PostalImport")
		@ResponseBody
		public Msg impotr(HttpServletRequest request, Model model) throws Exception {
		     //获取上传的文件 
		     MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
		     MultipartFile file = multipart.getFile("upfile");
		     InputStream in = file.getInputStream();
		     //数据导入  
		     postalService.importExcelInfo(in,file);
		     in.close();
		     return Msg.success();
		}
		
		/**
		 * 导出Excel
		 * @throws UnsupportedEncodingException 
		 * */
		@RequestMapping("/PostalExport")  
		public @ResponseBody void export(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException, UnsupportedEncodingException {  
			String postalData = "邮单信息表";
		    if(postalData!=""){
				response.reset(); //清除buffer缓存  
		        Map<String,Object> map=new HashMap<String,Object>();  
		        // 指定下载的文件名  
		        response.setContentType("application/octet-stream;charset=utf-8");  
		        response.setHeader("Content-Disposition", "attachment;filename="  
					     + new String(postalData.getBytes(),"iso-8859-1") + ".xlsx");
		        response.setHeader("Pragma", "no-cache");  
		        response.setHeader("Cache-Control", "no-cache");  
		        response.setDateHeader("Expires", 0);   
		        XSSFWorkbook workbook=null;  
		        //导出Excel对象  
		        workbook = postalService.exportExcelInfo(postalData);  
		        OutputStream output;  
		        try {
		            output = response.getOutputStream();  
		            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
		            bufferedOutPut.flush();  
		            workbook.write(bufferedOutPut);  
		            bufferedOutPut.close();  
		        } catch (IOException e) {  
		            e.printStackTrace();  
		        }  
		    }  
		}
}
